package top.bulk.dbcrypto.test.service.impl;

import org.springframework.stereotype.Service;
import top.bulk.dbcrypto.test.entity.User;
import top.bulk.dbcrypto.test.mapper.UserMapper;
import top.bulk.dbcrypto.test.service.UserService;
import top.bulk.dbcrypto.util.CryptoUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author 散装java
 * @since 2024-07-31 14:02:54
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userMapper.queryById(id);
    }

    @Override
    public List<User> queryList(Integer offset, Integer limit) {
        return this.userMapper.queryList(offset, limit);
    }


    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userMapper.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userMapper.deleteById(id) > 0;
    }

    @Override
    public String queryTelById(Integer id) {
        String tel = this.userMapper.queryTelById(id);
        return CryptoUtil.aesDecrypt(tel);
    }
}
